/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.application;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;

import com.pearreview.ui.BufferedImagePanel;
import com.pearreview.ui.CutCopyPasteManager;
import com.pearreview.ui.EscapeDialog;
import com.pearreview.ui.PearPanel;

public class AboutBox extends EscapeDialog implements HyperlinkListener {
	private static final long serialVersionUID = 7116420855999512730L;
	
	private PearPanel dialogPane;
	private JPanel contentPanel;
	private JPanel panel1;
	private JEditorPane text;
	private JPanel buttonBar;
	private JButton okButton;
	
	public AboutBox(Frame owner) {
		super(owner, true);
		initComponents();
	}

	public AboutBox(Dialog owner) {
		super(owner, true);
		initComponents();
	}

	private void initComponents() {
		setTitle(Messages.getString("PearReview.dialog.about"));
		
		dialogPane = new PearPanel();
		contentPanel = new JPanel();
		panel1 = new BufferedImagePanel(getClass()
				.getResource("images/about.png"));
		text = new JEditorPane();
		buttonBar = new JPanel();
		okButton = new JButton();

		//======== this ========
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		//======== contentPanel ========
		contentPanel.setOpaque(false);

		//======== buttonBar ========
		buttonBar.setOpaque(false);
		
		//======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				//---- label1 ----
				text.setContentType("text/html");
				text.setFont(new Font("Tahoma", Font.PLAIN, 12));
				text.setEditable(false);
				text.setText(
						"<html><body>" +
						"PearReview - The Peer Review Assistant. "+
						"Version " + PearReview.VERSION.getProductVersion() +
						"<br/> " +
						"Copyright &copy; 2009  " +
						"Dimitrij Pankratz, " +
						"Anton Musichin " 
						+
						"<br/>" +
						"Web: <a href='http://www.pearreview.com'>" +
						"http://www.pearreview.com</a> " +
						"<br/>" +
						"eMail: <a href='mailto:info@pearreview.com'>" +
						"info@pearreview.com</a><br/>" +
						"<br/>" +
						"Java: " + 
						System.getProperty("java.vendor") + ", " + 
						System.getProperty("java.version") + " (" + 
						System.getProperty("java.vm.name") + ", " +
						System.getProperty("java.vm.version") + ")" + "<br />" +
						"OS: " + 
						System.getProperty("os.name") + ", " + 
						System.getProperty("os.version") + ", " +
						System.getProperty("os.arch") + "<br />" +
						"</body></html>");
				text.setOpaque(false);
				text.addHyperlinkListener(this);
				String bodyRule = "body { font-family: Tahoma; " +
						"font-size: 12pt; }";
				String aRule = "a { font-family: Tahoma; " +
						"font-size: 12pt; color: #def933; }";
				((HTMLDocument)text.getDocument()).getStyleSheet()
						.addRule(bodyRule);
				((HTMLDocument)text.getDocument()).getStyleSheet()
						.addRule(aRule);

				text.setBorder(null);
				CutCopyPasteManager.getInstance().registerJTextComponent(text);

				GroupLayout contentPanelLayout = new GroupLayout(contentPanel);
				contentPanel.setLayout(contentPanelLayout);
				contentPanelLayout.setHorizontalGroup(
					contentPanelLayout.createParallelGroup()
						.addComponent(panel1, 
								GroupLayout.DEFAULT_SIZE, 
								500, 
								Short.MAX_VALUE)
						.addComponent(text, 
								GroupLayout.DEFAULT_SIZE, 
								500, 
								Short.MAX_VALUE)
				);
				contentPanelLayout.setVerticalGroup(
					contentPanelLayout.createParallelGroup()
						.addGroup(contentPanelLayout.createSequentialGroup()
							.addComponent(panel1, 
									GroupLayout.PREFERRED_SIZE, 
									302, 
									GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(text, 
									GroupLayout.DEFAULT_SIZE, 
									120, Short.MAX_VALUE)
							.addContainerGap())
				);
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== buttonBar ========
			{
				buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
				buttonBar.setLayout(new GridBagLayout());
				((GridBagLayout)buttonBar.getLayout()).columnWidths = 
					new int[] {0, 80};
				((GridBagLayout)buttonBar.getLayout()).columnWeights = 
					new double[] {1.0, 0.0};

				//---- okButton ----
				okButton.setText("OK");
				okButton.setFont(new Font("Tahoma", Font.PLAIN, 12));
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						AboutBox.this.dispose();
					}
				});
				buttonBar.add(okButton, 
						new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 0, 0), 0, 0));
			}
			dialogPane.add(buttonBar, BorderLayout.SOUTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(getOwner());
	}
	
	@Override
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			try {
				Desktop.getDesktop().browse(e.getURL().toURI());
			} catch(Exception ignore) {
			}
		}
	}
}
